package com.wslds.base.action;

import java.util.List;

import com.wslds.base.AbstractService;
import com.wslds.base.dao.page.PageAdapter;
import com.wslds.base.util.Strings;

/**
 * 
 * @author $Author: donilan.qq@gmail.com $
 * @version $Revision: 83 $
 */
@SuppressWarnings("serial")
public abstract class AbstractCRUDAction<T, S extends AbstractService<T>>
		extends BaseAction {

	protected T instance;
	protected List<T> instanceList;

	protected S service;

	public abstract void setService(S service);

	public String list() {
		if (this.getHttpRequest().getQueryString() != null) {
			this.setQueryString(Strings.cutPageQueryString(this
					.getHttpRequest().getQueryString()));
		}
		return SUCCESS;
	}

	public String listAjax() {
		if (this.getHttpRequest().getQueryString() != null) {
			this.setQueryString(Strings.cutPageQueryString(this
					.getHttpRequest().getQueryString()));
		}
		PageAdapter<T> adapter = service.page(instance, getPage());
		this.put("adapter", adapter);
		return SUCCESS;
	}

	/**
	 * 增加页面
	 */
	public String add() {
		List<T> instanceList = service.find(null);
		this.put("instanceList", instanceList);
		return SUCCESS;
	}

	/**
	 * 编辑页面
	 */
	public String edit() {
		instance = service.findOne(instance);
		List<T> instanceList = service.find(null);
		this.put("instanceList", instanceList);
		return SUCCESS;
	}

	/**
	 * 更新操作
	 */
	public String update() {
		service.update(instance);
		return REDRICT_TO_LIST;
	}

	/**
	 * 插入数据
	 */
	public String insert() {
		service.save(instance);
		return REDRICT_TO_LIST;
	}

	/**
	 * 列出所有
	 */
	public String listAll() {
		try {
			this.put("list", service.find(instance));
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public T getInstance() {
		return instance;
	}

	public void setInstance(T instance) {
		this.instance = instance;
	}

	public List<T> getInstanceList() {
		return instanceList;
	}

	public void setInstanceList(List<T> instanceList) {
		this.instanceList = instanceList;
	}
}
